package de.ars.client.animator;

import java.util.Enumeration;
import java.util.Vector;

import de.ars.client.items.animators.OverViewItemAnimator;
import de.ars.client.paintSystems.ClassicLayout;

public class Animator
{
    private Vector animatedPoints = null;
    protected IAnimationEventListener listener;
    
    public static final int ANIMATION_DONE = 0;
    public static final int ANIMATION_IN_PROGRESS = 1;
    
    private int animationState = ANIMATION_DONE;
    
    protected ClassicLayout layout;
    
    public Animator( IAnimationEventListener listener)
    {
        animatedPoints = new Vector();
        this.listener = listener;
    }
    
    public void setLayout(ClassicLayout layout)
    {
		this.layout = layout;
    }
    
    public void addPoint(float x, float y)
    {
        animatedPoints.addElement( new AnimatedPoint( x, y ) );
    }
    
    public void createNewAnimationPoint(Point p)
    {
        animatedPoints.addElement( new AnimatedPoint( p ) );
    }
    
    public Point getPoint(int index)
    {
        return ((AnimatedPoint)animatedPoints.elementAt( index )).getPoint();
    }
    
    public AnimatedPoint getAnimatedPoint(int index)
    {
        return (AnimatedPoint)animatedPoints.elementAt( index );
    }
    
    public void process(long elapsed)
    {
        if (animationState == ANIMATION_IN_PROGRESS)
        {
            Enumeration e = animatedPoints.elements();
            while (e.hasMoreElements())
                ((AnimatedPoint)e.nextElement()).process( elapsed );
            if (allAnimationsDone())
            {
                animationState = ANIMATION_DONE;
                listener.animationEvent(IAnimationEventListener.ANIMATION_STOPPED);
            }
        }
    }
    
    private boolean allAnimationsDone()
    {
        Enumeration e = animatedPoints.elements();
        while (e.hasMoreElements())
            if  (((AnimatedPoint)e.nextElement()).isAnimationInProgress( ))
                return false;
        return true;
    }
    
    public int getAnimationState()
    {
        return this.animationState;
    }
    
    public void startAnimationTo(int index, Point p )
    {
        startAnimationTo(index,p,OverViewItemAnimator.STANDARD_ANIMATION_SPEED );
    }
    
    public void startAnimationTo(int index, Point p,float animationSpeed )
    {
        startAnimationTo(index, p.getX(), p.getY(), animationSpeed );
    }
    
    public void startAnimationTo(int index, float x, float y,float animationSpeed )
    {
        animationState = ANIMATION_IN_PROGRESS;
        AnimatedPoint aP = (AnimatedPoint)animatedPoints.elementAt( index );
        if (!aP.isAnimationInProgress())
            aP.startAnimationTo( x, y,animationSpeed );
    }
    
    public boolean setPointTo(int index, Point p)
    {
        return setPointTo(index, p.getX(), p.getY());
    }
    
    public boolean setPointTo(int index, float x, float y)
    {
        AnimatedPoint aP = (AnimatedPoint)animatedPoints.elementAt( index );
        if (!aP.isAnimationInProgress())
        {
            aP.setPointTo( x, y );
            return true;
        }
        return false; 
    }
    
    public void startAnimationShrink(int index, float factor )
    {
        startAnimationShrink(index, factor, OverViewItemAnimator.STANDARD_ANIMATION_SPEED );
    }
    
    public void startAnimationShrink(int index, float factor,float animationSpeed )
    {
        animationState = ANIMATION_IN_PROGRESS;
        AnimatedPoint aP = (AnimatedPoint)animatedPoints.elementAt( index );
        if (!aP.isAnimationInProgress())
            aP.startAnimationShrink( factor,animationSpeed );
    }
    
    public void resetPosition(boolean setToTarget)
    {
        Enumeration e = animatedPoints.elements();
        while (e.hasMoreElements())
            ((AnimatedPoint)e.nextElement()).resetPosition( setToTarget );
    }
}
